<?php

    require_once('init.php');
    if(!Conf::isConnecter()) {
        Tools::redirect('inscription.php');
    }
    
    //On regarde si le formulaire a été envoyé
    if(Tools::getValue('envoie')) {
        if(Tools::getValue('id_civilite') && Tools::getValue('prenom') && Tools::getValue('nom') && Tools::getValue('email')) {
            $_SESSION['client']->setCivilite(Civilite::rechercheCivilite(Tools::getValue('id_civilite')));
            $_SESSION['client']->setPrenom(Tools::getValue('prenom'));
            $_SESSION['client']->setNom(Tools::getValue('nom'));
            $_SESSION['client']->setTelephone(Tools::getValue('telephone'));
            $_SESSION['client']->setMobile(Tools::getValue('mobile'));
            $_SESSION['client']->setEmail(Tools::getValue('email'));
            $_SESSION['client']->setDateNaissance(Tools::getValue('date_naissance'));
            $_SESSION['client']->update();
            Tools::redirect('index.php');
        }
        else {
            echo    'Veuillez rentrer tout les champs obligatoires';
        }
    }
    //On regarde si l'utilisateur veut changer de mot de passe
    if(Tools::getValue('changement_mdp')) {
        if(Tools::encrypt(Tools::getValue('ancien_mdp')) === $_SESSION['client']->getMdp()) {
            if(Tools::getValue('nouveau_mdp') == Tools::getValue('confirm_mdp')) {
                $_SESSION['client']->changementMdp(Tools::getValue('nouveau_mdp'));
            }
            else {
                echo    'Confirmation échoué';
            }
        }
        else {
            echo    'Mot de passe actuel invalide';
        }
    }
    require_once('header.php');
?>

<div class="coordonnees">
    <h2>COORDONNEES</h2>
    <div class="coordonnees_sans_titre">
    <form method="POST" action="moncompte.php">
        <label class="label_gerer">
            Civilité
        </label>
        <label class="label_gerer_valeur">
            <select name="id_civilite">
    <?php
        $civilites = Civilite::getCivilites();
        echo '<option value="'.$_SESSION['client']->getCivilite()->getId().'">'.$_SESSION['client']->getCivilite().'</option>';
        foreach($civilites as $civilite) {
            echo    '<option value="'.$civilite->getId().'">'.$civilite.'</option>';
        }
    ?>
            </select>
        </label>
        <label class="label_gerer">
            Prénom
        </label>
        <label class="label_gerer_valeur">
            <input type="text" name="prenom" value="<?php echo $_SESSION['client']->getPrenom(); ?>" size="30" maxlength="100" /> *
        </label><br>
        <label class="label_gerer">
            Nom
        </label>
        <label class="label_gerer_valeur">
            <input type="text" name="nom" value="<?php echo $_SESSION['client']->getNom(); ?>" size="30" maxlength="100" /> *
        </label><br>
        <label class="label_gerer">
            N° de téléphone
        </label>
        <label class="label_gerer_valeur">
            <input type="text" name="telephone" value="<?php echo $_SESSION['client']->getTelephone(); ?>" size="14" maxlength="14" 
                   data-format="tel" />
        </label><br>
        <label class="label_gerer">
            N° de mobile
        </label>
        <label class="label_gerer_valeur">
            <input type="text" name="mobile" value="<?php echo $_SESSION['client']->getMobile(); ?>" size="14" maxlength="14" 
                   data-format="tel" />
        </label><br>
        <label class="label_gerer">
            Email
        </label>
        <label class="label_gerer_valeur">
            <input type="text" name="email" value="<?php echo $_SESSION['client']->getEmail(); ?>" size="30" maxlength="150" 
                   data-format="email" /> *
        </label><br>
        <label class="label_gerer">
            Date de naissance
        </label>
        <label class="label_gerer_valeur">
            <input type="text" placeholder="dd/mm/YYYY" name="date_naissance" value="<?php echo $_SESSION['client']->getDateNaissance('d/m/Y'); ?>" 
                   size="30" maxlength="150" data-format="date" />
        </label><br>
        <label class="label_gerer">
            &nbsp;
        </label>
        <label class="label_gerer_valeur">
            <input  id="bouton_modifier_coordonnees"type="submit" name="envoie" value="Modifier" />
        </label><br><br>
        <label style="clear:both;"></label>        <br>

    </form>
    <a id="ajouter_adresse"href="ajoutAdresse.php">Ajouter ou modifier une adresse</a>
    </div>
</div>

           <p /> <h2 id="titre_gestion_compte">GESTION DU COMPTE</h2>
    <div class="gestion_compte">

    <span class="label_gerer">
        Mot de passe actuel  
    </span>
    <span class="label_gerer_valeur">
        <input type="password" name="ancien_mdp" size="30"/>
    </span>
    <span class="label_gerer">
        Nouveau mot de passe  
    </span>
    <span class="label_gerer_valeur">
        <input type="password" name="nouveau_mdp" size="30"/>
    </span>
    <span class="label_gerer">
        Confirmer  
    </span>
    <span class="label_gerer_valeur">
        <input type="password" name="confirm_mdp" size="30"/>
    </span>
    <span class="label_gerer">
        &nbsp;
    </span>
    <span class="label_gerer_valeur">
        <input id="bouton_modifier_compte" type="submit" name="changement_mdp" value="Modifier"/>
    </span>
    </div>
</div>

<?php
    require 'footer.php';
?>

